//
//  HomeViewController.swift
//  七哥直播
//
//  Created by seven on 2017/11/9.
//  Copyright © 2017年 seven. All rights reserved.
//

import UIKit

class HomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
}

//MARK:- 设置UI界面
extension HomeViewController{
    private func setupUI() {
        setupContentView()
        setupNavgationBar()
    }
    
    private func setupNavgationBar() {
        //左侧item
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "home-logo"), style: .plain, target: nil, action: nil)
        //右侧item
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "search_btn_follow"), style: .plain, target: self, action: #selector(collectionItemClick))
        //事件监听 ----> 发送消息 -----> 将方法包装SEL -----> 类方法列表 ----> IMP
        //搜索框
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 42))
        let searchFrame = CGRect(x: 0, y: 0, width: 200, height: 42)
        let searchBar = UISearchBar(frame: searchFrame)
        searchBar.placeholder = "主播昵称/房间号/链接"
        searchBar.searchBarStyle = .minimal
        titleView.addSubview(searchBar)
        navigationItem.titleView = titleView
        
        let searchTextField = searchBar.value(forKey: "_searchField") as? UITextField
        searchTextField?.textColor = .white
    }
    
    private func setupContentView(){
        let homeTypes = loadHomeTypeData()
        let style = LXTitleStyle()
        style.isScrollEnable = true
        style.isShowIndicatorLine = true
//        let pageFrame = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH - kNavigationBarH - kStatusBarH)
        let titles = homeTypes.map { $0.title }
        var childVcs = [AnchorViewController]()
        homeTypes.forEach { (homeType) in
            let anchorVc = AnchorViewController()
            anchorVc.homeType = homeType
            childVcs.append(anchorVc)
        }
        let pageView = LXContainerView(frame: view.bounds, titles: titles, childVcs: childVcs, parentVc: self, style: style)
        view.addSubview(pageView)
        
    }
    
    private func loadHomeTypeData() -> [HomeType]{
        let path = Bundle.main.path(forResource: "types.plist", ofType: nil)!
        let dataArr = NSArray(contentsOfFile: path) as! [[String:Any]]
        var tempArr = [HomeType]()
        tempArr = dataArr.map({ (dict) -> HomeType in
            let title = dict["title"] as! String
            let type = dict["type"] as! Int
            return HomeType(title: title, type: type)
        })
        return tempArr
    }
}

//MARK:- 点击事件/事件监听
extension HomeViewController{
    @objc private func collectionItemClick() {
        print("收藏")
    }
}
